package com.skeleton.adminServer.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.skeleton.adminServer.user.service.IAdminUserService;
import com.skeleton.adminServer.user.util.AdminUserConstants;
import com.skeleton.common.base.dto.ResultDto;
import com.skeleton.common.base.service.impl.BaseServiceImpl;
import com.skeleton.db.dao.IUserinfoDao;
import com.skeleton.db.dto.PageDto;
import com.skeleton.db.dto.UserinfoDto;
import com.skeleton.db.entity.Userinfo;

@Service
public class AdminUserServiceImpl extends BaseServiceImpl implements IAdminUserService {

	@Resource
	private IUserinfoDao iUserinfoDao;

	@Override
	public ResultDto doLogin(UserinfoDto uDto) {
		logger.debug("-------UserServiceImpl---doLogin-----------------");
		ResultDto rd = new ResultDto();

		Userinfo uEntity = uDto.toEntity();
		Userinfo u = iUserinfoDao.selectUserinfoByLogin(uEntity);

		if (u == null) {
			rd.setSuccess(false);
			rd.setErrorMSG("用户名或密码错误");
		} else if (u.getType() != AdminUserConstants.USER_TYPE_ADMIN) {
			rd.setSuccess(false);
			rd.setErrorMSG("你没有管理员权限");
		} else {
			UserinfoDto ud = u.toDto();
			rd.setSuccess(true);
			rd.addToDtoMap("UserinfoDto", ud);
		}

		return rd;
	}

	@Override
	public ResultDto getAllUserByPage(PageDto pDto) {
		logger.debug("-------UserServiceImpl---getAllUserByPage-----------------");
		ResultDto rd = new ResultDto();

		List<Userinfo> uList = iUserinfoDao.selectAllByPage(pDto);
		for (int i = 0; i < uList.size(); i++) {
			rd.addToDtoMap("UserinfoDtoList", uList.get(i).toDto());
		}

		int count = iUserinfoDao.countAll();
		pDto.setTotal(count);
		rd.addToDtoMap("PageDto", pDto);

		rd.setSuccess(true);
		return rd;
	}

}
